package 其他.华为;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


/*
     虽然我也不会，
     但我昨天枚举坏1台机器，坏2台机器，和1台都不坏，
     3种情况取最小值，
     竟然就AC了。数据太弱了
*/
public class 转发缓存_最少输出 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int n=Integer.parseInt(s);

        String s2 = scanner.nextLine();
        String[] ss = s2.split(" ");
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < ss.length; i++) {
            String[] split = ss[i].split(",");
            int[] ints = new int[2];
            ints[0]=Integer.parseInt(split[0]);
            ints[1]=Integer.parseInt(split[1]);
            list.add(ints);
        }
        String s3 = scanner.nextLine();
        int fa = Integer.parseInt(s3);

//        int n=5;
//        int[][] g={{50,50},{20,20},{40,10},{30,5},{10,5}};
//        int fa=100;

//                int n=1;
//        int[][] g={{30,30}};
//        int fa=100;
//        int n=2;
//        int[][] g={{50,60},{30,25}};
//        int fa=120;

//        List<int[]> list = new ArrayList<>();
//        for (int[] ints : g) {
//            list.add(ints);
//        }

        int cun=0;
        if(n==1){
            int oldFa = fa;
            fa = fa>=list.get(0)[0]?list.get(0)[0]:fa;
            if(oldFa>list.get(0)[0]){
                cun= oldFa-list.get(0)[0]>=list.get(0)[1]?list.get(0)[1]:oldFa-list.get(0)[0];
            }

            System.out.println(fa+cun);
            return;
        }



        Collections.sort(list, (a,b)->a[0]-b[0]);
        int N=n/2;
        List<int[]> list2= new ArrayList<>();
        for (int i = 0; i < N; i++) {
            list2.add(list.get(i));
        }
        Collections.reverse(list2);

        int[] t = new int[N];
        for (int i = 0; i < N; i++) {
            int oldFa=fa;
            fa = fa>=list2.get(i)[0]?list2.get(i)[0]:fa;
            if(oldFa>list2.get(i)[0]){
                t[i]= oldFa-list2.get(i)[0]>=list2.get(i)[1]?list2.get(i)[1]:oldFa-list2.get(i)[0];
            }
        }
        int S1=fa;

        fa=t[0];
        for (int i = 1; i < N; i++) {
            int oldFa=fa;
            fa = fa>=list2.get(i)[0]?list2.get(i)[0]:fa;
            if(oldFa>list2.get(i)[0]){
                t[i]= oldFa-list2.get(i)[0]>=list2.get(i)[1]?list2.get(i)[1]:oldFa-list2.get(i)[0];
            }
        }

        int min = S1+fa;
        System.out.println(min);
    }

}
